package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.node.TreeBack;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.Classification;
import cn.stylefeng.guns.modular.system.model.params.ClassificationParam;
import cn.stylefeng.guns.modular.system.service.ClassificationService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.node.TreeNode;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.Max;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;


/**
 * 产品分类表控制器
 *
 * @author 小飞
 * @Date 2019-06-25 17:06:21
 */
@Controller
@RequestMapping("/classification")
public class ClassificationController extends BaseController {

    private String PREFIX = "/classification";

    Logger logger = LoggerFactory.getLogger(ClassificationController.class);

    @Autowired
    private ClassificationService classificationService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-25
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/classification.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-25
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/classification_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-25
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/classification_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-25
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ClassificationParam classificationParam) {
        this.classificationService.add(classificationParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-25
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ClassificationParam classificationParam) {
        this.classificationService.update(classificationParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-25
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ClassificationParam classificationParam) {
        this.classificationService.delete(classificationParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-25
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ClassificationParam classificationParam) {
        Classification detail = this.classificationService.getById(classificationParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-25
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ClassificationParam classificationParam) {
        return this.classificationService.findPageBySpec(classificationParam);
    }


    /**
     * 获取树结构
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTree")
    public List<ZTreeNode> getTree(){

        List<ZTreeNode> treeNodes = this.classificationService.tree();
        treeNodes.add(ZTreeNode.createParent());

    return  treeNodes;
    }

    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public TreeBack search(@RequestParam("name") String name) throws UnsupportedEncodingException {
        name = URLDecoder.decode(name, "UTF-8");
        logger.info("解码:" + name);
        if (StringUtils.isEmpty(name)) {
            return new TreeBack(0, "ok", classificationService.list());
        } else {
            QueryWrapper<Classification> areaQueryWrapper = new QueryWrapper<>();
            areaQueryWrapper.like("name", name);
            return new TreeBack(0, "ok", classificationService.list(areaQueryWrapper));
        }
    }



}


